package com.dingding.special.user.driver.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.Userorder;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.beans.special.Specialuservehicleinfo;
import com.dingding.common.core.beans.special.Userspecialorder;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.special.trasfer.today_info.ReqGetTodayInfo;
import com.dingding.facade.special.trasfer.today_info.ResGetTodayInfo;
import com.dingding.order.code.order.OrderStatus;
import com.dingding.order.code.order.SpecialOrderStatus;
import com.dingding.special.user.driver.dao.IDriverUserDao;
import com.dingding.special.user.driver.dto.driverauth.ReqSpecialDriverAuth;
import com.dingding.special.user.driver.dto.driverauth.ResSpecialDriverAuth;
import com.dingding.special.user.driver.dto.reauth.ReqReDriverAuth;
import com.dingding.special.user.driver.dto.reauth.ResReDriverAuth;
import com.dingding.special.user.driver.enums.CarAuthStatus;
import com.dingding.special.user.driver.service.IDriverUserService;

/**
 * @Description:司机端
 * @author fengshuonan
 * @date 2016年8月19日 下午4:38:12
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("driverUserService")
public class DriverUserServiceImpl implements IDriverUserService{

	@Resource(name = "driverUserDao")
	private IDriverUserDao driverUserDao;
	
	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResGetTodayInfo getDriverTodayInfo(ReqGetTodayInfo reqGetTodayInfo) {
		
		String userId = reqGetTodayInfo.getUserId();
		
		//验证参数是否为空
		if(ToolUtil.isBlank(userId)){
			return null;
		}
		
		ResGetTodayInfo resGetTodayInfo = new ResGetTodayInfo();
		
		//获取在线时间
		resGetTodayInfo.setOnLineTime(this.driverUserDao.getTodayDrivingHours(userId));
		
		//获取当前出车状态
		resGetTodayInfo.setSupStatus(this.driverUserDao.getOutCarStatus(userId));
		
		//获取流水，成交率和接单次数
		List<Userspecialorder> allOrders = this.driverUserDao.getTodayAllDealOrder(userId);
		
		BigDecimal totalMoney = new BigDecimal("0.0");
		int dealOrderNum = 0;	//已完成订单
		int orderSumNum = 0;	//订单总数	
		BigDecimal interestRate = new BigDecimal("0.000"); //接单率 
		
		for(Userspecialorder order : allOrders){
			if(order.getOrderStatus() == SpecialOrderStatus.ORDER_OK){

				if (order.getTotalMoney() != null) {
					totalMoney = totalMoney.add(order.getTotalMoney());
				}
				dealOrderNum += 1;
			}
			orderSumNum += 1;
		}
		
		if(dealOrderNum != 0 && orderSumNum != 0){
			interestRate = new BigDecimal(dealOrderNum).divide(new BigDecimal(orderSumNum),3,RoundingMode.HALF_UP);
		}
		resGetTodayInfo.setCompletionTurnover(totalMoney.setScale(1,RoundingMode.HALF_UP).doubleValue());
		resGetTodayInfo.setRurnoverRate(ToolUtil.keepPlaces(interestRate.doubleValue() * 100) + "%");
		resGetTodayInfo.setTakeOrderNumber(orderSumNum);//接单次数为总单数
		return resGetTodayInfo;
	}


	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResSpecialDriverAuth specialDriverAuth(ReqSpecialDriverAuth reqSpecialDriverAuth) {
		
		ResSpecialDriverAuth resSpecialDriverAuth = new ResSpecialDriverAuth();
		
		//把车辆认证信息存入数据库
		Specialuservehicleinfo specialuservehicleinfo = this.driverUserDao.saveSpecialUserVehicleInfo(reqSpecialDriverAuth);
		//resSpecialDriverAuth.setJszImage(ToolUtil.imageAddress(specialuservehicleinfo.getJszImage()));
		resSpecialDriverAuth.setXszImage(ToolUtil.imageAddress(specialuservehicleinfo.getXszImage()));
		resSpecialDriverAuth.setVehicleImage(ToolUtil.imageAddress(specialuservehicleinfo.getVehicleImage()));
		resSpecialDriverAuth.setSuvStatus(specialuservehicleinfo.getSuvStatus());
		
		Specialuserlogin specialuserlogin = this.baseDaoX.getById(specialuservehicleinfo.getUserId(), Specialuserlogin.class);
		if(specialuserlogin == null){
			return resSpecialDriverAuth;
		}else{
			specialuserlogin.setUserRole(2);
			return resSpecialDriverAuth;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResReDriverAuth reDriverAuth(ReqReDriverAuth reqReDriverAuth) {
		
		//改变车辆审核的状态为重新认证状态
		boolean flag = this.driverUserDao.changeCarAuthStatus(reqReDriverAuth.getUserId(), CarAuthStatus.RE_AUTH);
		if(flag){
			return new ResReDriverAuth(CarAuthStatus.RE_AUTH.getCode());
		}else{
			return null;
		}
	}

	/**
	 * 
	 * @Title: getTaxiDriverTodayInfo 
	 * @Description: 获取出租车司机本日信息
	 * @param reqGetTodayInfo 获取当日司机信息
	 * @author YHQ 
	 * @return ResGetTodayInfo    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public ResGetTodayInfo getTaxiDriverTodayInfo(ReqGetTodayInfo reqGetTodayInfo) {
		
		String userId = reqGetTodayInfo.getUserId();
		
		//验证参数是否为空
		if(ToolUtil.isBlank(userId)){
			return null;
		}
		
		ResGetTodayInfo resGetTodayInfo = new ResGetTodayInfo();
		
		//获取在线时间
		resGetTodayInfo.setOnLineTime(this.driverUserDao.getTodayDrivingHours(userId));
		
		//获取当前出车状态
		resGetTodayInfo.setSupStatus(this.driverUserDao.getOutCarStatus(userId));
		
		//获取流水，成交率和接单次数
		List<Userorder> allOrders = this.driverUserDao.getTodayAllDealTaxiOrder(userId);
		
		BigDecimal totalMoney = new BigDecimal("0.0");
		int dealOrderNum = 0;	//已完成订单
		int orderSumNum = 0;	//订单总数	
		BigDecimal interestRate = new BigDecimal("0.000"); //接单率 
		
		for(Userorder order : allOrders){
			if(order.getOrderStatus() == OrderStatus.ORDER_STATUS_COMPLETE){

				if (order.getTotalMoney() != null) {
					totalMoney = totalMoney.add(order.getTotalMoney());
				}
				dealOrderNum += 1;
			}
			orderSumNum += 1;
		}
		
		if(dealOrderNum != 0 && orderSumNum != 0){
			interestRate = new BigDecimal(dealOrderNum).divide(new BigDecimal(orderSumNum),3,RoundingMode.HALF_UP);
		}
		resGetTodayInfo.setCompletionTurnover(totalMoney.setScale(1,RoundingMode.HALF_UP).doubleValue());
		resGetTodayInfo.setRurnoverRate(ToolUtil.keepPlaces(interestRate.doubleValue() * 100) + "%");
		resGetTodayInfo.setTakeOrderNumber(orderSumNum);//接单次数为总单数
		
		return resGetTodayInfo;
	}
	
	public static void main(String[] args) {
		Double double1 = new Double(111);
		System.out.println(double1 == null);
	}
}
